package com.wbk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.wbk.constant.MessageConstant;
import com.wbk.constant.RedisMessageConstant;
import com.wbk.entity.Result;
import com.wbk.pojo.Order;
import com.wbk.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private OrderService orderService;

    @RequestMapping("/submit")
    public Result submit(@RequestBody Map map){
        //验证码校验
        String telephone = (String)map.get("telephone");
        String validateCode = (String)map.get("validateCode");
        String validateCodeInRedis = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_ORDER);
        if (validateCodeInRedis!=null&&validateCodeInRedis.equals(validateCode)){
            //设置预约类型，微信预约，电话预约
            map.put("orderType", Order.ORDERTYPE_WEIXIN);
            Result result =null;
           try {
               result=orderService.order(map);
               return result;
           }catch (Exception e){
               e.printStackTrace();
               result=new Result(false,"预约失败");
               return result;
           }

        }else {
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            Map map = orderService.findById(id);
            //查询预约信息成功
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,map);
        }catch (Exception e){
            e.printStackTrace();
            //查询预约信息失败
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }
}
